library Wave10b initializer init requires LegacySystem, DeltaUtils

	globals
		private location udg_AirstrikeTarget
	endglobals
// aegis turrets
    private function M242AmmunitionFeedConditions2 takes nothing returns boolean
        return GetUnitTypeId(GetTriggerUnit())=='h00K'
    endfunction
    
    private function M242AmmunitionSystemConditions2 takes nothing returns boolean
        return GetUnitTypeId(GetAttacker())=='h00K'
    endfunction
    
    private function M242AmmunitionFeed2 takes nothing returns nothing
        local item i = GetManipulatedItem()
        local unit u = GetTriggerUnit()
        local integer id = GetItemTypeId(i)
        if id=='I006' or id=='shas' then
            call PauseUnit(u,false)
            call MachineGunLink(u)
            call SetUnitState(u,UNIT_STATE_MANA,GetUnitState(u,UNIT_STATE_MANA)+(50*I2R(GetItemCharges(i))))
            call AddFadingTextTag("+"+I2S(50*GetItemCharges(i)),GetUnitX(u),GetUnitY(u),255,220,0,200)
            call RemoveItem(i)
        else
            call UnitRemoveItem(u,i)
        endif
        set i = null
        set u = null
    endfunction

    private function M242AmmunitionSystem2 takes nothing returns nothing
        local unit u = GetAttacker()
        local real rounds = GetUnitState(u,UNIT_STATE_MANA)
        if rounds<0.5 then
            call IssueImmediateOrder(u,"stop")
        else
            if rounds-1.0<0 then
                set rounds=0
            else
                set rounds=rounds-1.0
            endif
            call SetUnitState(u,UNIT_STATE_MANA,rounds)
        endif
        set u = null
    endfunction
// end aegis turrets

    private function SSgtThompsonAI takes nothing returns nothing
        local location l = null
        local real life 
		
		if udg_DeltaLeader == null then
			call DisableTrigger(GetTriggeringTrigger())
			return 
		endif

		set life = GetWidgetLife(udg_DeltaLeader)
		
		if life <= 0.405 then
			call DisableTrigger(GetTriggeringTrigger())
			return
		endif
		
        if life<=150.0 and life>0.405 then
            if getInteger(null,"NumCorpsman")>0 and GetRandomInt(0,1)==1 then
                call GeneralText(1.5, "|cfff3ad00SSgt. Thompson|r : Corpsman!" )
            else
                call GeneralText(2.0, "|cfff3ad00SSgt. Thompson|r : I need medical attention." )
            endif
            set l = GetUnitLoc(udg_Hero[udg_Sorted[GetRandomInt(1,udg_Players)]])
            call IssuePointOrderLoc(udg_DeltaLeader,"patrol",l)
            call IssuePointOrderLoc(udg_DeltaOperator[2],"patrol",l)
            call RemoveLocation(l)
        elseif RectContainsCoords(udg_CoreObjective,GetUnitX(udg_DeltaLeader),GetUnitY(udg_DeltaLeader))==false then
            set l = GetRandomLocInRect(udg_CoreObjective)
            if GetRandomInt(0,5)!=1 then
                call IssuePointOrderLoc(udg_DeltaLeader,"attack",l)
                call IssuePointOrderLoc(udg_DeltaOperator[2],"attack",l)
            else
                call IssuePointOrderLoc(udg_DeltaLeader,"move",l)
                call IssuePointOrderLoc(udg_DeltaOperator[2],"move",l)
            endif
            call RemoveLocation(l)
        else
            if udg_ExtractionComplete==false then
                if GetRandomInt(0,2)==1 then
                    call IssuePointOrder(udg_DeltaLeader,"attack",12500.0,6700.0)
                elseif GetRandomInt(0,2)==1 then
                    call IssuePointOrder(udg_DeltaLeader,"attack",11800.0,9000.0)
                elseif GetRandomInt(0,4)==1 then
                    call IssuePointOrder(udg_DeltaLeader,"attack",13000.0,12000.0)
                endif
            else
                call IssueImmediateOrder(udg_DeltaLeader, "holdposition")
            endif
        endif
        set l = null
    endfunction

    private function DeltaOperatorAI takes nothing returns nothing
        local integer i = 1
        local real x
        local real y
        local real dx
        local real dy
		local real life = 0.0

		if udg_DeadDeltas>=5 then
			call DisableTrigger(GetTriggeringTrigger())
			return
		endif
		
		if udg_DeltaLeader != null then
			set life = GetWidgetLife(udg_DeltaLeader)
		endif		

        loop
            exitwhen i > 5
            call ItemAI(udg_DeltaOperator[i])
            set i = i + 1
        endloop
        if life<=0.405 then
            set x = GetUnitX(udg_Hero[udg_Sorted[GetRandomInt(1, udg_Players)]])+10.0
            set y = GetUnitX(udg_Hero[udg_Sorted[GetRandomInt(1, udg_Players)]])+10.0
            call IssuePointOrder(udg_DeltaOperator[2],"attack",x,y)
        else
            set x = GetUnitX(udg_DeltaLeader)+10.0
            set y = GetUnitY(udg_DeltaLeader)-10.0
        endif
        set i = 3
        loop
            exitwhen i > 5
            if GetRandomInt(0,1)==1 then
                call PolledWait(GetRandomReal(0.0,1.5))
            endif
            set dx = GetUnitX(udg_DeltaOperator[i]) - x
            set dy = GetUnitY(udg_DeltaOperator[i]) - y
            if RectContainsCoords(udg_CoreObjective,GetUnitX(udg_DeltaOperator[i]),GetUnitX(udg_DeltaOperator[i]))==false then        
                if dx*dx+dy*dy>=360000.0 then //600 distance from Thompson
                    call IssueImmediateOrder(udg_DeltaOperator[i],"berserk")
                    call IssuePointOrder(udg_DeltaOperator[i],"move",x,y)
                else
                    call IssuePointOrder(udg_DeltaOperator[i],"attack",x,y)
                endif
            elseif udg_ExtractionComplete==false then
                if GetRandomInt(0,2)==1 then
                    call IssuePointOrder( udg_DeltaOperator[i], "attack", 12500.0, 6700.0 )
                elseif GetRandomInt(0,2)==1 then
                    call IssuePointOrder( udg_DeltaOperator[i], "attack", 11800.0, 9000.0 )
                elseif GetRandomInt(0,4)==1 then
                    call IssuePointOrder( udg_DeltaOperator[i], "attack", 13000.0, 12000.0 )
                endif
            endif
            set i = i + 1
        endloop
    endfunction


    /*
    
    GlobalUnit 20      : Delta Leader
    GlobalUnit 21-24 : Delta Force
    GlobalUnit [40] - Communications Array

    GetUnitState(udg_DeltaLeader,UNIT_STATE_LIFE) > 0

    GlobalUnit 20      : Delta Leader
    GlobalUnit 21-24 : Delta Force

    */
    
    private function DeltaInitialize takes nothing returns nothing
        local trigger t1
        local trigger t2
        
        set t1 = CreateTrigger()
        call TriggerAddAction(t1,function SSgtThompsonAI)
        
        set t2 = CreateTrigger()
        call TriggerAddAction( t2, function DeltaOperatorAI )
        
        call RemoveLocation(udg_LZPoint)
        call RemoveLocation(udg_AirstrikeTarget)
        if udg_ExtractionComplete then
            set udg_CoreObjective = udg_LandingZones[GetRandomInt(1,8)]
        else
            set udg_CoreObjective = Rect(12256.0, 6112.0, 13664.0, 12928.0)
        endif
        call TriggerRegisterTimerEvent(t2, 8.0, true)
        call SetSmartGrenadesEnabled(true)
        call PolledWait(6.)
        call TriggerRegisterTimerEvent(t1, 10.0, true)
        call RadioChat( udg_DeltaLeader, 5.0, "|cfff3ad00SSgt. Thompson|r : We're Delta. We're here to assist you until Fox Company arrives." )
        call PolledWait(6.)
        call RadioChat( udg_DeltaLeader, 5.0, "|cfff3ad00SSgt. Thompson|r : Stick with us. We'll pull through this together." )
    endfunction

    private function LyganeshSequence takes nothing returns nothing
        local location t = GetUnitLoc(udg_Lyganesh)
        local trigger this_trigger = GetTriggeringTrigger()
        local group g = AMGetUnitsInRange(300, udg_AirstrikeTarget, Condition(function SelectAlliedUnits))
        local integer i = 1
        loop
            exitwhen i>5
            if RectContainsCoords(udg_CoreObjective,GetUnitX(udg_DeltaOperator[i]),GetUnitY(udg_DeltaOperator[i])) then
                call IssuePointOrder(udg_DeltaOperator[i],"move",GetUnitX(udg_DeltaOperator[i])+200.0, GetUnitY(udg_DeltaOperator[i])+200.0 )
            endif
            set i=i+1
        endloop
        if GetWidgetLife(udg_Lyganesh)<=0.405 then
            call DisableTrigger(this_trigger)
            if GetRandomInt(0,2)==1 then
                call RadioChat(udg_DeltaLeader, 5.0, "|cfff3ad00SSgt. Thompson|r : You boys make us look like fifth tires! Hell of a job." )
            endif
            call DeltaInitialize.execute()
            call DestroyGroup(g)
            call RemoveLocation(t)
            set t = null
            set g = null
            return
        endif
        if udg_SquadXP>80000 then
            //Free-roam
        elseif GetRandomInt(0,1)==1 then
            call IssuePointOrderLoc(udg_Lyganesh,"attack",udg_AirstrikeTarget)
        endif
        if DistanceBetweenPoints(t,udg_AirstrikeTarget)<=310.0 and FirstOfGroup(g)==null then
            call DisableTrigger(this_trigger)
            if GetWidgetLife(udg_Lyganesh)>2500.0 then
                call GeneralText(2.0, "|cfff3ad00Delta Operator|r : Fire in the Hole!" )
                call PolledWait(0.2)
                call IssuePointOrderLoc(udg_Lyganesh,"attack",udg_AirstrikeTarget)
                call PolledWait(0.2)
                call IssuePointOrderLoc(udg_Lyganesh,"attack",udg_AirstrikeTarget)
                call UnitAddAbility(udg_DeltaOperator[2],'A038')
                call IssuePointOrderLoc(udg_DeltaOperator[2], "flamestrike",udg_AirstrikeTarget)
                call EarShockArea(GetLocationX(udg_AirstrikeTarget),GetLocationY(udg_AirstrikeTarget),500)
                call PolledWait(2.)
                call UnitRemoveAbility(udg_DeltaOperator[2],'A038')
            else
                call GeneralText(3.0, "|cfff3ad00Delta Operator|r : Looks like he's done for." )
            endif
            call DeltaInitialize.execute()
            set udg_GlobalBoolean[3] = true
        endif
        call DestroyGroup(g)
        call RemoveLocation(t)
        set t = null
        set g = null
    endfunction
  
    private function DeltaCasualty takes nothing returns nothing
        local integer i = 2
        local unit u = GetTriggerUnit()
        local player killer = GetOwningPlayer(GetKillingUnit())
        set udg_DeadDeltas = udg_DeadDeltas + 1
        loop
            exitwhen i>5
            if GetTriggerUnit()==udg_DeltaOperator[i] then
                set udg_DeltaOperator[i] = null
            endif
            set i=i+1
        endloop
        if udg_DeadDeltas>=5 then
            //call DestroyTrigger(gg_trg_Delta_Force_AI)
            //call DestroyTrigger(gg_trg_Dead_Deltas)
        endif
        set u = null
    endfunction

    private function SSgtThompsonKilled takes nothing returns nothing
        call DisableTrigger(GetTriggeringTrigger())
        call ALICEText(5.0, "|cfff3ad00ALICE|r : Staff Sergeant Thompson has been killed in action." )
        set udg_DeltaLeader = null
        set udg_DeltaOperator[1] = null
        set udg_DeadDeltas = udg_DeadDeltas + 1
		// call DestroyTrigger(gg_trg_Delta_Wave_Spawn)
        //call DestroyTrigger(gg_trg_Delta_Leader_AI) // no need since inner trigger handles that now
    endfunction

    private function DeltaDeploymentConditions takes nothing returns boolean
        local integer i = GetUnitTypeId(GetTriggerUnit())
        return (i=='N006' and GetUnitState(GetTriggerUnit(),UNIT_STATE_LIFE)<=29000.0) or (i=='N00F' and GetUnitState(GetTriggerUnit(),UNIT_STATE_LIFE)<=35000.0) or GetEventDamageSource()==udg_Residence41
    endfunction

    private function DeltaLeaderVision takes nothing returns nothing
        call UnitShareVision( udg_DeltaLeader, GetEnumPlayer(), true )
    endfunction

    private function DeltaDeployment takes nothing returns nothing
        local integer i = 3
        local integer chainsplanted = 15
        local location l
        local location lyga
        local real x
        local real y
        local trigger t 
        
        call DisableTrigger(GetTriggeringTrigger())
        if udg_SquadXP<100000 then
            call SetUnitMoveSpeed(udg_Lyganesh,GetUnitDefaultMoveSpeed(udg_Lyganesh)*0.75)
        endif
        call PolledWait(2.)
        set lyga = GetUnitLoc(udg_Lyganesh)
        call SetDynamicEvasionEnabled(true)
        call StartSound(gg_snd_Delta)
        set l = SelectPoint(lyga,1000,1200)
        set udg_DeltaLeader = CreateUnit(Player(9),'z007',GetLocationX(l),GetLocationY(l),0.0) //Delta Leader
        call RemoveLocation(l)
        
        set t = CreateTrigger()
        call TriggerRegisterUnitEvent(t,udg_DeltaLeader,EVENT_UNIT_DEATH)
        call TriggerAddAction( t, function SSgtThompsonKilled )
        
        call UnitAddItemById(udg_DeltaLeader,'I001') //Composite
        call UnitAddItemById(udg_DeltaLeader,'I007') //SAM-R
        call UnitAddItemById(udg_DeltaLeader,'I004')
        call UnitAddItemById(udg_DeltaLeader,'phea')
        call SetLoadState(I2S(GetHandleId(udg_DeltaLeader)),1)
        call SetUnitUserData(udg_DeltaLeader,69)
        set udg_DeltaOperator[1] = udg_DeltaLeader
        call TriggerSleepAction(0.)
        
        set t = CreateTrigger()
        loop
            exitwhen i>5
            if i==2 then
                set i=3
            endif
            set l = SelectPoint(lyga,1000,1400)
            set udg_DeltaOperator[i] = CreateUnit(Player(9),'z005',GetLocationX(l),GetLocationY(l),0.0) //Delta Operator
            call SetLoadState(I2S(GetHandleId(udg_DeltaOperator[i])),1)
            call UnitAddItemById(udg_DeltaOperator[i],'I004')
            call UnitAddItemById(udg_DeltaOperator[i],'I001') //Composite
            call SetUnitUserData(udg_DeltaOperator[i], 69)
            
            
            call TriggerAddAction( t, function DeltaCasualty )
            call TriggerRegisterUnitEvent(t,udg_DeltaOperator[i],EVENT_UNIT_DEATH)
            
            
            call RemoveLocation(l)
            call TriggerSleepAction(0.)
            set i=i+1
        endloop
        // This is the Operator who nades Lyga
        set udg_LZPoint = SelectPoint(lyga,1100,1500)
        set udg_DeltaOperator[2] = CreateUnit(Player(9),'z005',GetLocationX(udg_LZPoint),GetLocationY(udg_LZPoint),0.0) //Delta Operator
        call SetLoadState(I2S(GetHandleId(udg_DeltaOperator[2])),1)
        call UnitAddItemById(udg_DeltaOperator[2],'I004')
        call UnitAddItemById(udg_DeltaOperator[i],'I001') //Composite
        call SetUnitUserData(udg_DeltaOperator[2],69)
        call SetUnitPathing(udg_DeltaOperator[2],false)
        
        call TriggerRegisterUnitEvent(t,udg_DeltaOperator[2],EVENT_UNIT_DEATH)
        
        call ForForce(udg_EchoCompany,function DeltaLeaderVision)
        call IssueTargetOrder(udg_DeltaLeader,"faeriefire",udg_Lyganesh)
        call RemoveLocation(lyga)
        set lyga = GetUnitLoc(udg_Lyganesh)
        set l = Location(GetUnitX(udg_DeltaOperator[2])+(0.5*(GetUnitX(udg_Lyganesh)-GetUnitX(udg_DeltaOperator[2]))), GetUnitY(udg_DeltaOperator[2])+(0.5*(GetUnitY(udg_Lyganesh)-GetUnitY(udg_DeltaOperator[2]))) )
        call IssuePointOrderLoc(udg_DeltaOperator[2],"move",l)
        call PolledWait(3.)
        set udg_AirstrikeTarget = GetUnitLoc(udg_DeltaOperator[2])
        set x = GetUnitX(udg_DeltaOperator[2])
        set y = GetUnitY(udg_DeltaOperator[2])
        if udg_SquadXP>120000 then
            set chainsplanted = 5
        elseif udg_SquadXP>80000 then
            set chainsplanted = 8
        endif
        set i=1
        loop
            exitwhen i>chainsplanted
            call CreateUnit(Player(9),'n008',x,y,0.0)
            if GetRandomInt(0,1)==1 then
                call TriggerSleepAction(0.)
            endif
            set i=i+1
        endloop
        call StartSound(gg_snd_Defend)
        call GeneralText(2.5, "|cfff3ad00Delta Operator|r : Explosives in place." )
        set udg_CoreObjective = RectFromCenterSizeBJ(udg_AirstrikeTarget, 500.0, 500.0)
        call IssuePointOrderLoc(udg_DeltaOperator[2],"move",udg_LZPoint)
        call SetUnitPathing(udg_DeltaOperator[2],true)
        call SetUnitMoveSpeed(udg_Lyganesh,GetUnitDefaultMoveSpeed(udg_Lyganesh))
        
        set t = CreateTrigger()
        call TriggerAddAction(t,function LyganeshSequence)
        call TriggerRegisterTimerEvent(t,0.45,true)
        
        call RemoveLocation(lyga)
        call RemoveLocation(l)
        set lyga = null
        set l = null
        set t = null
    endfunction

    private function MarineCommandChapter2 takes nothing returns nothing
        call SetPlayerTechResearched(GetEnumPlayer(),'R004',1) //Gunship Support
        call SetPlayerTechResearched(GetEnumPlayer(),'R00Z',1) //IUL Fire Mission
    endfunction
    
    private function Wave10Creeps takes nothing returns nothing
        call PlatoonReinforcements.execute()
        set udg_ACA[12] = true
        call ForForce(udg_EchoCompany,function MarineCommandChapter2)
        call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 5.0, "0320 Local Time" )
        call C130Wave10.execute()
        if udg_Mode==4 and udg_Players>0 then
            call ALICEText(10.0, "Chapter 1 Complete. |cff1e90ff100 XP|r awarded to " + GetPlayerName(ConvertedPlayer(udg_Sorted[1])) )
            set udg_Experience[udg_Sorted[1]] = udg_Experience[udg_Sorted[1]] + 100
        endif
        call PolledWait(30.0*udg_Pace)
        call SpawnRectRandom(gg_rct_SpawnEastCave,Player(14),'n004',udg_Spawn)
        call SpawnRectRandom(gg_rct_SpawnEastBottom,Player(13),'n003',udg_Spawn)
        call SpawnRectRandom(gg_rct_SpawnNorthWaterfall,Player(14),'z002',udg_Spawn)
        call SpawnRectRandom(gg_rct_SpawnTownWestCave,Player(13),'n004',2+udg_Spawn)
        call SpawnRectRandom(gg_rct_SpawnSouthWaterfall,Player(14),'n004',2+udg_Spawn)
        call RandomSpawn('u000',2,GetRandomInt(0,udg_Spawn)) //Futty
        call SpawnRectRandom(gg_rct_SpawnWestTop,Player(14),'n004',2+udg_Spawn)
        call RandomSpawn('z00C',2,GetRandomInt(0,1)) //Assasin
        call RandomSpawn('n002',1,GetRandomInt(0,udg_Spawn)) //Devourer
        if udg_Players==udg_InitialPlayers then
            call GankMarine(Player(13),'u005',2+udg_Spawn,1600,2000)
        endif
        call SpawnRectRandom(gg_rct_SpawnEastCave,Player(14),'nsw1',udg_InitialPlayers)
        call GankMarine(Player(13),'n004',2+udg_Spawn,1600,2000)
        call GankMarine(Player(13),'n004',2+udg_Spawn,1600,2000)
        call PolledWait(30.0*udg_Pace)
        call RandomSpawn('u00L',4,IMinBJ(5, udg_Players)) //Blink
        call GankMarine(Player(14),'n004',2+udg_Spawn,1600,2000)
        call GankMarine(Player(14),'n004',2+udg_Spawn,1600,2000)
        if udg_Players>=6 then
            call RandomSpawn('ugar',1,2)
            call RandomSpawn('n002',1,2)
        endif
        call PolledWait(30.0*udg_Pace)
        call RandomSpawn('ugar',1,GetRandomInt(1,udg_Spawn)) //Gargoyle
        call GankMarine(Player(13),'n005',2+udg_Spawn,1600,2000)
    endfunction
        
    private function Wave10b takes nothing returns nothing
        local real x = GetRandomReal(-2000.0,10000.0)
        local real y = GetRandomReal(-1000.0,12000.0)
        local integer i = 1
        local real trans = 0.0
        local trigger t
      
        
        if udg_Players<1 or udg_Chapter==2 then
            return
        endif
        if GetRandomInt(0,10)==1 then
            set trans = 40.0
        endif
        call SetPlayerName( Player(9), "1st SFOD-D" )
        set udg_Intermission = true
        set udg_Lighting = true
        call SquadRatingCalc.execute()
        set udg_XPFactor = udg_XPFactor * 1.1
        set udg_Chapter = 2
        set udg_M242Components[1] = 'I00Q'
        set udg_M242Components[2] = 'I00R'
        set udg_M242Components[3] = 'I00P'
        set udg_M242Components[4] = 'I00S'
        call PauseAllUnitsBJ(true)
        call CinematicFadeBJ( bj_CINEFADETYPE_FADEOUT, 1.0, "ReplaceableTextures\\CameraMasks\\Black_mask.blp", 0, 0, 0, trans )
        call ShowInterface(false,1.0)
        call ClearTextMessages()
        call PolledWait(1.5)
        call StartSound(gg_snd_Message)
        call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 5.0, "                                                                                     |cff8b864eDelta|r" )
        call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 5.0, " " )
        call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 5.0, " " )
        call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 5.0, " " )
        call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 5.0, " " )
        call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 5.0, " " )
        call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 5.0, " " )
        call PauseAllUnitsBJ(true)
        call PolledWait(5.5)
        call CinematicFadeBJ( bj_CINEFADETYPE_FADEIN, 1.0, "ReplaceableTextures\\CameraMasks\\Black_mask.blp", 0, 0, 0, trans )
        call ShowInterface(true,1.0)
        call ClearTextMessages()
        set udg_Lighting = false
        call PolledWait(1.)
        set udg_Intermission = false
        call PauseAllUnitsBJ(false)
        call IntermissionRestore()
        call PolledWait(5.)
        call CinematicFadeBJ( bj_CINEFADETYPE_FADEOUT, 0.001, "ReplaceableTextures\\CameraMasks\\Black_mask.blp", 0, 0, 0, 0 )
        call CinematicFadeBJ( bj_CINEFADETYPE_FADEIN, 0.001, "ReplaceableTextures\\CameraMasks\\Black_mask.blp", 0, 0, 0, 0 )
        call TriggerSleepAction(0.)
        call IntermissionRestore()
        call SetImmortalSpawnEnabled(true)
        call SetCrepitusSpawnEnabled(true)
        call SetGhoulSpawnEnabled(true)
        call SetRevelationSpawnEnabled(true)
        
        set t = CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_PICKUP_ITEM )
        call TriggerAddCondition( t, Condition( function M242AmmunitionFeedConditions2 ) )
        call TriggerAddAction( t, function M242AmmunitionFeed2 )
        
        set t = CreateTrigger()
        call TriggerRegisterPlayerUnitEvent(t, Player(13), EVENT_PLAYER_UNIT_ATTACKED, null)
        call TriggerRegisterPlayerUnitEvent(t, Player(14), EVENT_PLAYER_UNIT_ATTACKED, null)
        call TriggerRegisterPlayerUnitEvent(t, Player(9), EVENT_PLAYER_UNIT_ATTACKED, null)
        call TriggerAddCondition( t, Condition( function M242AmmunitionSystemConditions2 ) )
        call TriggerAddAction( t, function M242AmmunitionSystem2 )
        
        call PolledWait(10.*udg_Pace)
        if GetRandomInt(0,2)==1 then
            set udg_ExtractionComplete = true //M242
        else
            set udg_ExtractionComplete = false //Airfield defense
        endif
        set udg_CurrentWave = 10
        call GenericActions.execute()
        call MBSet( udg_SquadStatus, 1, 1, "Objective : Secure Perimeter" )
        call QuestSetDescription(udg_CurrentOrders, "A reactionary force is on its way. Echo Company must maintain the strong point." )
        call ALICEText(5., "|cff8b864eNew Objective|r : Secure Perimeter." )
        set udg_GlobalBoolean[3] = false
        if udg_Players<=4 or udg_SquadXP<40000 then
            set udg_Lyganesh = CreateUnit(Player(14),'N006',x,y,0.0) //Lyganesh A
        else
            set udg_Lyganesh = CreateUnit(Player(14),'N00F',x,y,0.0) //Lyganesh B
        endif
        call SetUnitPathing(udg_Lyganesh,false)
        call SuspendHeroXP(udg_Lyganesh, true)
        
        set t = CreateTrigger()
        call TriggerAddCondition( t, Condition( function DeltaDeploymentConditions ) )
        call TriggerAddAction(t,function DeltaDeployment)
        call TriggerRegisterUnitEvent( t, udg_Lyganesh, EVENT_UNIT_DAMAGED )
        
        call PolledWait(10.)
        call SetUnitPathing(udg_Lyganesh,true)
        call Wave10Creeps.execute()
        call PolledWait(180.)
        if udg_Players>0 then
            if GetWidgetLife(udg_DeltaLeader)>0.405 then
                if udg_ExtractionComplete then
                    call ExecuteRegisteredFunction("Wave11b")
                    //call Wave11b.execute()
                else
                    call ExecuteRegisteredFunction("Wave11c")
                    //call Wave11c.execute()
                endif
            else
				call CleanUpDeltaTriggers()
                //call DisableTrigger(gg_trg_Delta_Leader_AI)
                //call DisableTrigger(gg_trg_Delta_Force_AI)
                //call DisableTrigger(gg_trg_Delta_Leader_Killed)
               // call DisableTrigger(gg_trg_Dead_Deltas)
                call DisableTrigger(t)
                call SetDeltaSpawnEnabled(false)
                call DeltaExit.execute()
                call ExecuteRegisteredFunction("Wave11d")
                //call Wave11d.execute()
            endif
        endif
    endfunction

    private function init takes nothing returns nothing
        call RegisterFunction("Wave10b", function Wave10b)
        //call ExecuteRegisteredFunction("Wave10b")
    endfunction
endlibrary